package Servers

import (
	//"GameLogic/LogicDefine"
	"PBMessage"
	. "SGNet"

	. "misc/EventSystem"
	proto "misc/protobuf/proto"
	log "misc/seelog"
)

func (sceneServer *SceneServer) LoadHandlers() {
	sceneServer.AddNetHandler(PBMessage.PBID_CS_BackToHall, sceneServer.CS_BackToHallHandler)
	sceneServer.AddNetHandler(PBMessage.PBID_CS_AbilityEvent, sceneServer.CS_AbilityEvent)
	sceneServer.AddNetHandler(PBMessage.PBID_CS_EnterGameSceneOK, sceneServer.CS_EnterSceneOK)
}

func (sceneServer *SceneServer) CS_BackToHallHandler(messge *Message) {
	backHall := &PBMessage.SC_GameEndBackHall{}
	backHall.Exp = proto.Int32(10)
	backHall.Gold = proto.Int32(10)
	scbackHall := &PBMessage.CS_BackToHall{}
	proto.Unmarshal(messge.Data, scbackHall)
	scenePlayer := sceneServer.Players[scbackHall.GetUserId()]
	if scenePlayer != nil {
		scenePlayer.GetSeesion().SendPacket(PBMessage.PBID_SC_GameEndBackHall, backHall)
	} else {
		log.Info("scenePlayer is nil", sceneServer.Players, scbackHall.GetUserId())
	}
	sceneServer.RemovePlayer(scenePlayer.GetUserId())
	sceneServer.GateServer.PushMessage(CommondMessage(CommonID_PlayerBackToHall, "Player", scenePlayer))
	if len(sceneServer.Players) == 0 {
		sceneServer.Destory()
	}
}

func (sceneServer *SceneServer) CS_AbilityEvent(messge *Message) {
	sceneServer.LogicScene.UnitAbilityEventHandler(messge)
}

func (sceneServer *SceneServer) CS_EnterSceneOK(message *Message) {
	GlobalEventManager.CallEvent("GameScene.Player.EnterOK")
}
